from common import FeaturedArtistEntity,SearchFilter
import DatabaseConnection

class FeaturedArtistData(object):
    def __init__(self):
        pass

    def InsertFeaturedArtist(self,featuredArtist):
        conn = DatabaseConnection.GetConnection()

        cursor = conn.cursor()
        cursor.callproc('InsFeaturedArtist',(featuredArtist.SongID,featuredArtist.ArtistID))
        cursor.close()
        conn.commit()
        conn.close()

    def UpdateFeaturedArtist(self,featuredArtist):
        conn = DatabaseConnection.GetConnection()

        cursor = conn.cursor()
        cursor.callproc('UpdFeaturedArtist',(featuredArtist.SongID,featuredArtist.ArtistID))
        cursor.close()
        conn.commit()
        conn.close()

        pass

    def DeleteFeaturedArtist(self,sID, aID):
        conn = DatabaseConnection.GetConnection()

        cursor = conn.cursor()
        cursor.callproc('DelFeaturedArtist',(sID,aID,))
        cursor.close()
        conn.commit()
        conn.close()

    #This method either return an object of type FeaturedArtistEntity or None (if there is no record with this ID)
    def SelectFeaturedArtistByID(self,ID):
        query = """ SELECT SongID,ArtistID,SongName,ArtistName
                    FROM vwFeaturedArtist
                    WHERE Username='%s'""" % ID

        conn = DatabaseConnection.GetConnection()

        cursor = conn.cursor()
        cursor.execute(query)
        result = cursor.fetchall()
        cursor.close()
        conn.close()
                
        if len(result) <> 0:
            return MemberEntity.MemberEntity(songID=result[0][0],artistID=result[0][1],songName=result[0][2],artistName=result[0][3])
        else:
            return None

    #This method returns a list of objects of type FeaturedArtistEntity
    def SelectFeaturedArtistByFilter(self,searchFilter):
        query = """ SELECT SongID,ArtistID,SongName,ArtistName
                    FROM vwFeaturedArtist"""

        whereClause = searchFilter.GetWhereClause()
        if whereClause.strip()<>"":
            query += " WHERE %s" % whereClause

        conn = DatabaseConnection.GetConnection()

        cursor = conn.cursor()
        cursor.execute(query)
        result = cursor.fetchall()
        cursor.close()
        conn.close()
                
        recordSet = []
        for record in result:
            recordSet.append(MemberEntity.MemberEntity(songID=record[0],artistID=record[1],songName=record[2],artistName=record[3])
        return recordSet

    def SelectAllFeaturedArtists(self):
        query = """ SELECT SongID,ArtistID,SongName,ArtistName
                    FROM vwFeaturedArtist"""

        conn = DatabaseConnection.GetConnection()

        cursor = conn.cursor()
        cursor.execute(query)
        result = cursor.fetchall()
        cursor.close()
        conn.close()
                
        recordSet = []
        for record in result:
            recordSet.append(MemberEntity.MemberEntity(songID=record[0],artistID=record[1],songName=record[2],artistName=record[3])
        return recordSet
